package com.haredot.controller;

import com.haredot.constraint.SysConst;
import com.haredot.dto.PasswordDTO;
import com.haredot.entity.User;
import com.haredot.service.IndexService;
import com.haredot.vo.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

@Controller
public class IndexController {

    @Resource
    private IndexService indexService;

    /**
     * 系统主题框架
     *
     * @return
     */
    @GetMapping("/")
    public String index(HttpSession session, ModelMap modelMap) {
        User current = (User) session.getAttribute(SysConst.CURRENT_USER);
        modelMap.addAttribute("current", current);
        return "index";
    }

    /**
     * 欢迎页面(首页)
     *
     * @return
     */
    @GetMapping("/welcome")
    public String welcome(ModelMap modelMap) {
        Map<String, Integer> map = indexService.queryStudentAndTeacherCount();
        modelMap.addAllAttributes(map);
        return "welcome";
    }

    /**
     * 查询欢迎页需要的数据
     * @return
     */
    @GetMapping("/data")
    @ResponseBody
    public R<?> indexData() {
        Map<String, Object> data = indexService.queryData();
        return R.<Map<String, Object>>ok().data(data);
    }

    @PutMapping("/modifyPass")
    @ResponseBody
    public R<?> modifyPassword(@RequestBody @Valid PasswordDTO passwordDTO, HttpSession session) {
        User current = (User) session.getAttribute(SysConst.CURRENT_USER);

        return indexService.modifyPassword(current, passwordDTO);
    }

    /**
     * 查看个人资料
     *
     * @return
     */
    @GetMapping("/modify/password")
    public String modifyPassword() {
        return "modifyPassword";
    }
}
